interface ObserverItf {
  nameChange(name: string): any;
}

export class Person {
  private _name: string;

  // 观察者数组
  observers: ObserverItf[] = [];

  set name(name: string) {
    this._name = name;
    // 遍历观察者数组，给所有的观察者发送消息
    for (let v of this.observers) {
      v.nameChange(this._name);
    };
  }
  get name(): string {
    return this._name;
  }
}

class Test implements ObserverItf {
  nameChange(name: string) {
    console.log(`姓名发生改变了，改变的内容是${name}`);
  }
}

let p = new Person();
let t = new Test();

// 给p对象中的观察者添加对象
p.observers.push(t);

p.name = "李白";